package cn.bgs.service.impl;

import cn.bgs.controller.DormitoryController;
import cn.bgs.dao.dormitoryMapper;
import cn.bgs.exception.PhoenixException;
import cn.bgs.pojo.StudentDormitoryRelation;
import cn.bgs.pojo.StudentGradeRelation;
import cn.bgs.pojo.dormitory;
import cn.bgs.service.dormitoryService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.maven.plugin.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import java.util.Calendar;
import java.util.List;

/**
 * @ProjectName: bei
 * @Package: cn.bgs.service.impl
 * @ClassName: dormitoryServiceImpl
 * @Author: Administrator
 * @Description:
 * @Date: 2021/8/11 0011 19:45
 * @Version: 1.0
 */
@Service
public class dormitoryServiceImpl implements dormitoryService {
    @Autowired
    private dormitoryMapper dormitoryMapper;
    Logger log = LogManager.getLogger();
    @Override
    public List<dormitory> findormitory(String area, String bedNumber, String buildingFloor, String buildingNo, String dormitoryName, String dormSupervisor) {
        List<dormitory> dorm = dormitoryMapper.findormitory(area,bedNumber,buildingFloor,buildingNo,dormitoryName,dormSupervisor);
        for(dormitory i:dorm){

            Integer number = dormitoryMapper.dormitoryCount(i.getDormitoryId());

            String txt=number+"(已用)"+"/"+i.getBedNumber()+"(总数)";

            i.setBedNumbers(txt);
        }
        return dorm;
    }

    @Override
    public List<dormitory> area() {
        return dormitoryMapper.area();
    }

    @Override
    public Integer insertdormitory(dormitory dormitory) {
        return dormitoryMapper.insertSelective(dormitory);
    }

    @Override
    public Integer deleteByPkId(List<Integer> list) {
        return dormitoryMapper.deleteByPrimaryKey(list);
    }

    @Override
    public Integer dormitorydel(Integer pkId) {
        return dormitoryMapper.dormitorydel(pkId);
    }

    @Override
    public Integer updateByPkId(dormitory dormitory) {
        return dormitoryMapper.updateByPrimaryKey(dormitory);
    }

    @Override
    public dormitory dormitoryFind(Integer pkId) {
        return dormitoryMapper.selectByPrimaryKey(pkId);
    }

    @Override
    public Integer dormitoryAddStu(Integer[] ids, String dormitoryId, String dormitoryName) throws PhoenixException {
        try{
            int count=0;
            for(int i=0;i<ids.length;i++){
                StudentDormitoryRelation dormitoryRelation = new StudentDormitoryRelation();
                dormitoryRelation.setStudentId(ids[i].toString());
                dormitoryRelation.setDormitoryDormitoryId(dormitoryId);
                dormitoryRelation.setDormitoryDormitoryName(dormitoryName);
                dormitoryRelation.setDeleteFlag("0");

                //获取时间
                Calendar now = Calendar.getInstance();
                dormitoryRelation.setCreateTime(now.getTime());
                dormitoryRelation.setUpdateTime(now.getTime());

                log.error("gradeMapper.insertGradeStu:::" + dormitoryRelation);
                count+=dormitoryMapper.dormitoryAddStu(dormitoryRelation);

            }

            return count;
        }catch (DuplicateKeyException e) {

            log.error("gradeMapper.insertGradeStu:::" + e.getMessage());
            throw new PhoenixException("8888", "违反唯一索引");

        }
    }

    @Override
    public Integer dormitoryCount(String dormitoryId) {
        return dormitoryMapper.dormitoryCount(dormitoryId);
    }

    @Override
    public dormitory dormitoryFindSel(String dormitoryName) {
        return dormitoryMapper.dormitoryFindSel(dormitoryName);
    }
}
